package com.canaan.business.service.storehouse.impl;

import com.canaan.business.common.enums.agv.AgvEnum;
import com.canaan.business.common.enums.inOutStock.BillBindOperationEnum;
import com.canaan.business.common.enums.inOutStock.ErpBillTypeEnum;
import com.canaan.business.common.enums.inOutStock.InOrderStatusEnum;
import com.canaan.business.common.enums.inOutStock.OutOrderStatusEnum;
import com.canaan.business.common.enums.storehouse.StoreHouseTypeEnum;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.common.helper.BeanHelper;
import com.canaan.business.domain.agv.AgvTaskDispatch;
import com.canaan.business.domain.basic.StockStrategy;
import com.canaan.business.domain.basic.WmsStorehouse;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.bill.BillDetailRecord;
import com.canaan.business.domain.bill.Tray;
import com.canaan.business.domain.bill.TrayDetail;
import com.canaan.business.domain.inOutStock.OutStockBill;
import com.canaan.business.domain.inOutStock.OutStockBillDetail;
import com.canaan.business.domain.inOutStock.SplitBill;
import com.canaan.business.domain.inOutStock.SplitBillDetail;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.OutStockQueryVo;
import com.canaan.business.domain.vo.OutStockResultVo;
import com.canaan.business.domain.vo.TraySplitProductVo;
import com.canaan.business.domain.vo.WmsStorehouseStockVo;
import com.canaan.business.mapper.basic.WmsStorehouseLocationMapper;
import com.canaan.business.mapper.basic.WmsStorehouseMapper;
import com.canaan.business.mapper.storehouse.WmsStorehouseStockMapper;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import com.canaan.business.service.basic.IStockStrategyService;
import com.canaan.business.service.bill.ITrayDetailService;
import com.canaan.business.service.bill.ITrayService;
import com.canaan.business.service.inOutStock.ISplitBillDetailService;
import com.canaan.business.service.storehouse.IStorehouseStockStrategyService;
import com.canaan.business.service.storehouse.IWmsStockOutStockStrategyService;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StorehouseStockStrategyServiceImpl implements IStorehouseStockStrategyService {
    @Autowired
    private WmsStorehouseStockMapper storehouseStockMapper;
    @Autowired
    private WmsStorehouseMapper wmsStorehouseMapper;
    @Autowired
    private WmsStorehouseLocationMapper storehouseLocationMapper;
    @Autowired
    private ITrayService trayService;
    @Autowired
    private ITrayDetailService trayDetailService;
    @Autowired
    private IStockStrategyService stockStrategyService;
    @Resource
    private IAgvTaskDispatchService agvTaskDispatchService;
    @Autowired
    private BeanHelper beanHelper;
    @Autowired
    private ISysConfigService sysConfigService;
    @Resource
    private ISplitBillDetailService splitBillDetailService;
    @Override
    public WmsStorehouseStockVo selectEmptyLocationByMinRowMinColumn() {
        WmsStorehouse query=new WmsStorehouse();
        query.setTypeNo(StoreHouseTypeEnum.PRODUCT.getCode());
        query.setDelFlag("0");
        List<WmsStorehouse> wmsStorehouses=wmsStorehouseMapper.selectWmsStorehouseList(query);
        if(wmsStorehouses==null||wmsStorehouses.size()==0){
            throw new RuntimeException("成品仓不存在");
        }
        List<WmsStorehouseStockVo> storehouseStocks=storehouseStockMapper.selectEmpWmsStorehouseStockListMinRowMinColumn();
        if(storehouseStocks==null||storehouseStocks.size()==0){
            throw new RuntimeException("成品仓不存在空库位");
        }

        return storehouseStocks.get(0);
    }

    /**
     * 查询备料区的空库位
     * @return
     */
    @Override
    public List<WmsStorehouseStockVo> selectPreStorageEmpWmsStorehouseStockListMinRowMinColumn(){
        return storehouseStockMapper.selectPreStorageEmpWmsStorehouseStockListMinRowMinColumn();
    }

    /**
     * 指定库位旁边空库位
     * @param location 指定库位
     * @return 空库位
     */
    public WmsStorehouseStockVo selectNearByEmptyLocation(WmsStorehouseLocation location){
        List<WmsStorehouseStockVo> storehouseStocks=storehouseStockMapper.selectEmpWmsStorehouseStockListMinRowMinColumn();
        int min=Integer.MAX_VALUE;
        WmsStorehouseStockVo vo=null;
        for (WmsStorehouseStockVo item:storehouseStocks) {
            if(item.getStatus().equals(StorehouseStockStatusEnum.LOCK.getCode())) {
                continue;
            }
            if(item.getStorehouseLocationNo().equals(location.getInnerLocation())) {
                continue;
            }
            int interval=Math.abs(item.getRows()-location.getRows())+Math.abs(item.getColumns()-location.getColumns());
            if(min>interval){
                min=interval;
                vo=item;
            }
        }
        return vo;
    }

    /**
     * 获取所有AGV执行过程中托盘集合
     * @param outStockQueryVo
     * @return
     */
    private List<String> getTraysInBack(OutStockQueryVo outStockQueryVo){
        //不在库存中的托盘明细
        List<TrayDetail> trayDetails=trayDetailService.selectTrayDetailsByOutStockQueryVo(outStockQueryVo);
        if(trayDetails==null||trayDetails.size()==0){
            return new ArrayList<>();
        }
        List<String> trays=trayDetails.stream().map(TrayDetail::getTrayNo).distinct().collect(Collectors.toList());
        List<String> results=new ArrayList<>();
        //过滤出所有回库AGV任务的托盘
        for(String tray :trays){
            List<AgvTaskDispatch> agvTaskDispatches=agvTaskDispatchService.selectAgvTaskDispatchListByTrayNo(tray);
            if(agvTaskDispatches!=null&&agvTaskDispatches.size()!=0){
                AgvTaskDispatch agvTaskDispatch=agvTaskDispatches.get(0);
                if(AgvEnum.TASK_TYPE.BACK.getCode().equals(agvTaskDispatch.getTaskType())) {
                    results.add(tray);
                }
            }
        }
        return results;
    }

    /**
     * 解析已经存在于分拣单中托盘
     */
    private OutStockResultVo analyseExistSplit(OutStockQueryVo outStockQueryVo,Map<String,Long> productMap){
        List<TraySplitProductVo> traySplitProductVos=splitBillDetailService.selectSplitBillDetailListByOutStockQueryVo(outStockQueryVo);
        if(traySplitProductVos==null||traySplitProductVos.size()==0){
            return null;
        }
        OutStockResultVo resultVo=new OutStockResultVo();
        List<OutStockBill> outStockBills=new ArrayList<>();
        List<OutStockBillDetail> outStockBillDetails=new ArrayList<>();
        List<BillDetailRecord> billDetailRecords=new ArrayList<>();
        List<SplitBill> splitBills=new ArrayList<>();
        List<SplitBillDetail> splitBillDetails=new ArrayList<>();
        List<String> trays=traySplitProductVos.stream().map(TraySplitProductVo::getTrayNo).distinct().collect(Collectors.toList());
        //所有回库的托盘
        List<String> backTrays=getTraysInBack(outStockQueryVo);
        if(backTrays.size()!=0){
            trays.addAll(backTrays);
        }
        for(String tray:trays){
            String shelfNo="";
            String sourceLocation="";
            WmsStorehouseStock wmsStorehouseStock=storehouseStockMapper.selectWmsStorehouseStockByTrayNo(tray);
            if(wmsStorehouseStock==null){
                List<AgvTaskDispatch> agvTaskDispatches=agvTaskDispatchService.selectAgvTaskDispatchListByTrayNo(tray);
                if(agvTaskDispatches==null||agvTaskDispatches.size()==0){
                    throw new RuntimeException("托盘"+tray+"不存在AGV任务");
                }
                AgvTaskDispatch agvTaskDispatch=agvTaskDispatches.get(0);
                sourceLocation=agvTaskDispatch.getStartLocationCode();
                shelfNo=agvTaskDispatch.getShelfNo();
            }
            else{
                shelfNo=wmsStorehouseStock.getContainerNo();
                sourceLocation=wmsStorehouseStock.getStorehouseLocationNo();
            }
            OutStockBill outStockBill=new OutStockBill();
            outStockBill.setNo("OUT"+TableNoUtil.getNo(OutStockBill.class));
            outStockBill.setTrayNo(tray);
            outStockBill.setShelfNo(shelfNo);
            outStockBill.setBillType(outStockQueryVo.getBillType());
            outStockBill.setSourceLocation(sourceLocation);
            outStockBill.setAddress(outStockQueryVo.getCellCode());
            if(backTrays.contains(tray)){
                List<AgvTaskDispatch> agvTaskDispatches=agvTaskDispatchService.selectAgvTaskDispatchListByTrayNo(tray);
                if(agvTaskDispatches!=null&&agvTaskDispatches.size()!=0) {
                    AgvTaskDispatch agvTaskDispatch = agvTaskDispatches.get(0);
                    outStockBill.setSourceLocation(agvTaskDispatch.getEndLocationCode());
                    outStockBill.setDestLocation("");
                }
                outStockBill.setOrderStatus(OutOrderStatusEnum.NEW.getCode());
            }
            else {
                outStockBill.setOrderStatus(OutOrderStatusEnum.ACCOMPLISH.getCode());
            }
            outStockBill.setRelOrderNo(outStockQueryVo.getBillNo());
            outStockBill.setBillType(outStockQueryVo.getBillType());
            outStockBill.setIsSplit("1");
            TrayDetail query=new TrayDetail();
            query.setTrayNo(tray);
            query.setDelFlag("0");
            List<TrayDetail> trayDetails=trayDetailService.selectTrayDetailList(query);
            List<OutStockBillDetail> trayBillDetails=new ArrayList<>();
            for(TrayDetail trayDetail:trayDetails){
                if(!productMap.containsKey(trayDetail.getProductNo())){
                    continue;
                }
                Long usedQty=0l;
                if(!backTrays.contains(tray)){
                    //该托盘已经存在的分拣单明细,计算已经分拣的总数
                    List<TraySplitProductVo> splitProductVos = traySplitProductVos.stream().filter(O -> tray.equals(O.getTrayNo()) &&
                            trayDetail.getProductNo().equals(O.getProductNo())).collect(Collectors.toList());
                    usedQty = splitProductVos.stream().map(TraySplitProductVo::getSplitQty).mapToLong(Long::longValue).sum();
                }
                //托盘明细存在剩余
                if(trayDetail.getQty()>usedQty){
                    Long resCount=trayDetail.getQty()-usedQty;
                    Long requiredCount= productMap.get(trayDetail.getProductNo());
                    OutStockBillDetail detail = new OutStockBillDetail();
                    detail.setProductNo(trayDetail.getProductNo());
                    detail.setProductName(trayDetail.getProductName());
                    detail.setProductUnit(trayDetail.getProductUnit());
                    detail.setStatus(OutOrderStatusEnum.NEW.getCode());
                    detail.setNo(TableNoUtil.getNo(OutStockBillDetail.class));
                    detail.setBatchNo(trayDetail.getBatchNo());
                    detail.setOrderNo(outStockBill.getNo());
                    //需求数量小于剩下数量
                    if(requiredCount<=resCount){
                        detail.setQty(requiredCount);
                        productMap.remove(trayDetail.getProductNo());
                    }
                    else{
                        detail.setQty(resCount);
                        productMap.put(trayDetail.getProductNo(),requiredCount-resCount);
                    }

                    outStockBillDetails.add(detail);
                    trayBillDetails.add(detail);
                    //单据绑定纪录
                    BillDetailRecord record=new BillDetailRecord();
                    record.setStockStatus(trayDetail.getStockStatus());
                    record.setBatchNo(trayDetail.getBatchNo());
                    record.setBillType(outStockQueryVo.getBillType());
                    record.setBillNo(outStockQueryVo.getBillNo());
                    record.setTrayNo(tray);
                    record.setQty(detail.getQty());
                    record.setProductNo(trayDetail.getProductNo());
                    record.setProductUnit(trayDetail.getProductUnit());
                    record.setProductName(trayDetail.getProductName());
                    String operation= ErpBillTypeEnum.ADJUST_OUT.getCode().equals(outStockQueryVo.getBillType())?
                            BillBindOperationEnum.ADJUST_OUT.getMessage() : BillBindOperationEnum.PURCHASE_RETURN.getMessage();
                    record.setOperation(operation);
                    billDetailRecords.add(record);
                }
            }
            //创建分拣单
            if(trayBillDetails.size()!=0){
                outStockBills.add(outStockBill);
                SplitBill splitBill=new SplitBill();
                splitBill.setNo("S"+TableNoUtil.getNo(SplitBill.class));
                splitBill.setOrderStatus(InOrderStatusEnum.NEW.getCode());
                splitBill.setTrayNo(tray);
                splitBill.setShelfNo(shelfNo);
                splitBill.setRelOrderNo(outStockBill.getNo());
                //对应erp单据
                splitBill.setRemark(outStockQueryVo.getBillNo());
                splitBills.add(splitBill);
                for(OutStockBillDetail detail :trayBillDetails){
                    SplitBillDetail splitBillDetail=new SplitBillDetail();
                    splitBillDetail.setNo(TableNoUtil.getNo(SplitBillDetail.class));
                    splitBillDetail.setOrderNo(splitBill.getNo());
                    splitBillDetail.setProductNo(detail.getProductNo());
                    splitBillDetail.setProductName(detail.getProductName());
                    splitBillDetail.setProductUnit(detail.getProductUnit());
                    splitBillDetail.setBatchNo(detail.getBatchNo());
                    splitBillDetail.setQty(detail.getQty());
                    splitBillDetail.setStatus(OutOrderStatusEnum.NEW.getCode());
                    splitBillDetails.add(splitBillDetail);
                }
            }
        }
        resultVo.setOutStockBills(outStockBills);
        resultVo.setOutStockBillDetails(outStockBillDetails);
        resultVo.setSplitBills(splitBills);
        resultVo.setSplitBillDetails(splitBillDetails);
        resultVo.setBillDetailRecords(billDetailRecords);
        return resultVo;
    }
    /**
     * 查询指定物料和数量库存
     * @param outStockQueryVo 查询条件
     * @return 库存结果
     */
    public OutStockResultVo selectAllOutStock(OutStockQueryVo outStockQueryVo){
        //整理物料和需求数量
        Map<String,Long> resultMap=new HashMap<>();
        for(int i=0;i<outStockQueryVo.getProductNos().size();i++){
            String productNo=outStockQueryVo.getProductNos().get(i);
            Long count=outStockQueryVo.getProductCounts().get(i);
            resultMap.put(productNo,count);
        }
        //优先查询存在分拣单的物料
        OutStockResultVo splitResultVo=analyseExistSplit(outStockQueryVo,resultMap);
        //查询分拣后更新剩余数据
        if(splitResultVo!=null){
            for(int i=outStockQueryVo.getProductNos().size()-1;i>=0;i--){
                String productNo=outStockQueryVo.getProductNos().get(i);
                if(resultMap.containsKey(productNo)){
                    outStockQueryVo.getProductCounts().set(i,resultMap.get(productNo));
                }
                else{
                    if(outStockQueryVo.getProductCounts()!=null&&outStockQueryVo.getProductCounts().size()>i) {
                        outStockQueryVo.getProductCounts().remove(i);
                    }
                    if(outStockQueryVo.getBatchNos()!=null&&outStockQueryVo.getBatchNos().size()>i) {
                        outStockQueryVo.getBatchNos().remove(i);
                    }
                    outStockQueryVo.getProductNos().remove(i);
                }
            }
        }

        //剩余数据从现有库存查询
        List<WmsStorehouseStockVo> wmsStorehouseStockVos=new ArrayList<>();
        if(outStockQueryVo.getProductNos().size()!=0) {
            StockStrategy stockStrategy = stockStrategyService.selectOutStockDefaultStrategy();
            if (stockStrategy == null) {
                wmsStorehouseStockVos = storehouseStockMapper.selectWmsStorehouseStockListByProductNos(outStockQueryVo);
            } else {
                IWmsStockOutStockStrategyService outStockStrategyService = (IWmsStockOutStockStrategyService) beanHelper.getBean(stockStrategy.getClassName());
                if (outStockStrategyService == null) {
                    throw new RuntimeException("出库策略" + stockStrategy.getClassName() + "不存在");
                }
                wmsStorehouseStockVos = outStockStrategyService.selectWmsStorehouseStockListByProductNos(outStockQueryVo);
            }
            if (wmsStorehouseStockVos == null || wmsStorehouseStockVos.size() == 0) {
                throw new RuntimeException("成品库存不足");
            }

            if (outStockQueryVo.getProductNos().size() != outStockQueryVo.getProductCounts().size()) {
                throw new RuntimeException("物料与数量不一致");
            }
            //按照是否外库和数量排序
            wmsStorehouseStockVos.sort(Comparator.comparing((WmsStorehouseStockVo i)->i.getIsInner())
                    .thenComparing(i->i.getQty()).thenComparing(i->i.getBatchNo()));
        }
        OutStockResultVo outStockResultVo=new OutStockResultVo();
        List<WmsStorehouseStockVo> stockVos=new ArrayList<>();
        List<OutStockBill> outStockBills=new ArrayList<>();
        List<OutStockBillDetail> outStockBillDetails=new ArrayList<>();
        List<SplitBill> splitBills=new ArrayList<>();
        List<SplitBillDetail> splitBillDetails=new ArrayList<>();
        List<BillDetailRecord> billDetailRecords=new ArrayList<>();
        for (WmsStorehouseStockVo item :wmsStorehouseStockVos){
//            if("1".equals(item.getIsInner())&& StringUtils.isNotEmpty(item.getInnerLocation())){
//                WmsStorehouseStockVo outerStock=storehouseStockMapper.selectStorehouseStockByLocationNo(item.getInnerLocation());
//                if(StringUtils.isNotEmpty(outerStock.getContainerNo())){
//                    log.info("里库"+item.getStorehouseLocationNo()+"关联外库"+item.getInnerLocation()+"存在容器");
//                    continue;
//                }
//                if(!(StorehouseStockStatusEnum.NORMAL.getCode().equals(outerStock.getStatus()))){
//                    log.info("里库"+item.getStorehouseLocationNo()+"关联外库"+item.getInnerLocation()+"状态不是NORMAL");
//                    continue;
//                }
//            }
            TrayDetail query=new TrayDetail();
            query.setTrayNo(item.getTrayNo());
            query.setDelFlag("0");
            List<TrayDetail> trayDetails=trayDetailService.selectTrayDetailList(query);
            if(trayDetails==null||trayDetails.size()==0){
                continue;
            }
            if(resultMap.size()==0){
                break;
            }
            trayDetails=trayDetails.stream().sorted(Comparator.comparing(TrayDetail::getBatchNo)).collect(Collectors.toList());

            List<OutStockBillDetail> trayBillDetails=new ArrayList<>();
            OutStockBill outStockBill=new OutStockBill();
            outStockBill.setNo("OUT"+TableNoUtil.getNo(OutStockBill.class));
            outStockBill.setBillType(outStockQueryVo.getBillType());
            outStockBill.setAddress(outStockQueryVo.getCellCode());
            int trayTotal=0;
            int detailTotal=0;
            for(TrayDetail trayDetail:trayDetails) {
                trayTotal+=trayDetail.getQty();
                if(!resultMap.containsKey(trayDetail.getProductNo())){
                    continue;
                }
                Long resCount=resultMap.get(trayDetail.getProductNo());
                if(resCount==0){
                    continue;
                }
                OutStockBillDetail detail = new OutStockBillDetail();
                detail.setProductNo(trayDetail.getProductNo());
                detail.setProductName(trayDetail.getProductName());
                detail.setProductUnit(trayDetail.getProductUnit());
                detail.setStatus(OutOrderStatusEnum.NEW.getCode());
                detail.setNo(TableNoUtil.getNo(OutStockBillDetail.class));
                if(trayDetail.getQty()>=resCount) {
                    detail.setQty(resCount);
                    resultMap.remove(trayDetail.getProductNo());
                }
                else{
                    detail.setQty(trayDetail.getQty());
                    resultMap.put(trayDetail.getProductNo(),resCount-trayDetail.getQty());
                }
                detail.setBatchNo(trayDetail.getBatchNo());
                detail.setOrderNo(outStockBill.getNo());
                detailTotal+=detail.getQty();
                outStockBillDetails.add(detail);
                trayBillDetails.add(detail);
                //单据绑定纪录
                BillDetailRecord record=new BillDetailRecord();
                record.setStockStatus(item.getStockStatus());
                record.setBatchNo(trayDetail.getBatchNo());
                record.setBillType(outStockQueryVo.getBillType());
                record.setBillNo(outStockQueryVo.getBillNo());
                record.setTrayNo(item.getTrayNo());
                record.setQty(detail.getQty());
                record.setProductNo(trayDetail.getProductNo());
                record.setProductUnit(trayDetail.getProductUnit());
                record.setProductName(trayDetail.getProductName());
                String operation= ErpBillTypeEnum.ADJUST_OUT.getCode().equals(outStockQueryVo.getBillType())?
                        BillBindOperationEnum.ADJUST_OUT.getMessage() : BillBindOperationEnum.PURCHASE_RETURN.getMessage();
                record.setOperation(operation);
                billDetailRecords.add(record);
            }
            //托盘
            Tray tray=trayService.selectTrayByTrayNo(item.getTrayNo());
            if(tray!=null){
                tray.setOutBillNo(outStockBill.getNo());
                tray.setErpOutBillNo(outStockQueryVo.getBillNo());
            }
            if(detailTotal!=trayTotal&&detailTotal!=0){
                SplitBill splitBill=new SplitBill();
                splitBill.setNo("S"+TableNoUtil.getNo(SplitBill.class));
                splitBill.setOrderStatus(InOrderStatusEnum.NEW.getCode());
                splitBill.setTrayNo(item.getTrayNo());
                splitBill.setShelfNo(item.getContainerNo());
                splitBill.setRelOrderNo(outStockBill.getNo());
                splitBill.setRemark(outStockQueryVo.getBillNo());
                splitBills.add(splitBill);
                if(tray!=null){
                    tray.setSplitBillNo(splitBill.getNo());
                }
                for(OutStockBillDetail detail :trayBillDetails){
                    SplitBillDetail splitBillDetail=new SplitBillDetail();
                    splitBillDetail.setNo(TableNoUtil.getNo(SplitBillDetail.class));
                    splitBillDetail.setOrderNo(splitBill.getNo());
                    splitBillDetail.setProductNo(detail.getProductNo());
                    splitBillDetail.setProductName(detail.getProductName());
                    splitBillDetail.setProductUnit(detail.getProductUnit());
                    splitBillDetail.setBatchNo(detail.getBatchNo());
                    splitBillDetail.setQty(detail.getQty());
                    splitBillDetail.setStatus(OutOrderStatusEnum.NEW.getCode());
                    splitBillDetails.add(splitBillDetail);
                }
            }
            if(trayBillDetails.size()!=0){
                outStockBill.setRelOrderNo(outStockQueryVo.getBillNo());
                outStockBill.setTrayNo(item.getTrayNo());
                outStockBill.setSourceLocation(item.getStorehouseLocationNo());
                outStockBill.setShelfNo(item.getContainerNo());
                outStockBill.setOrderStatus(OutOrderStatusEnum.NEW.getCode());
                if(detailTotal!=trayTotal){
                    outStockBill.setIsSplit("1");
                }else{
                    outStockBill.setIsSplit("0");
                }
                stockVos.add(item);
                outStockBills.add(outStockBill);
            }
            if(tray!=null){
                trayService.updateTray(tray);
            }
        }
        if(resultMap.size()!=0){
            StringBuilder str= new StringBuilder();
           for(String productNo:resultMap.keySet()){
               str.append(productNo).append(":").append(resultMap.get(productNo)).append(";");
           }
           boolean shortMatchConfig=Boolean.parseBoolean(sysConfigService.selectConfigByKey("system.shortMatch"));
            if(!shortMatchConfig||"0".equals(outStockQueryVo.getShortMatch())) {
                throw new RuntimeException("成品库存不足，" + str);
            }
        }
        outStockResultVo.setOutStockBillDetails(new ArrayList<>());
        outStockResultVo.setOutStockBills(new ArrayList<>());
        outStockResultVo.setSplitBills(new ArrayList<>());
        outStockResultVo.setSplitBillDetails(new ArrayList<>());
        outStockResultVo.setBillDetailRecords(new ArrayList<>());
        if(splitResultVo!=null){
            outStockResultVo.getBillDetailRecords().addAll(splitResultVo.getBillDetailRecords());
            outStockResultVo.getOutStockBills().addAll(splitResultVo.getOutStockBills());
            outStockResultVo.getOutStockBillDetails().addAll(splitResultVo.getOutStockBillDetails());
            outStockResultVo.getSplitBills().addAll(splitResultVo.getSplitBills());
            outStockResultVo.getSplitBillDetails().addAll(splitResultVo.getSplitBillDetails());
        }
        outStockResultVo.getOutStockBills().addAll(outStockBills);
        outStockResultVo.getOutStockBillDetails().addAll(outStockBillDetails);
        outStockResultVo.getSplitBills().addAll(splitBills);
        outStockResultVo.getSplitBillDetails().addAll(splitBillDetails);
        //outStockResultVo.setWmsStorehouseStockVos(stockVos);
        outStockResultVo.getBillDetailRecords().addAll(billDetailRecords);
        return outStockResultVo;
    }
}
